//
//  HZMFeedbackVC.m
//  糖糖大魔王
//
//  Created by 韩震明 on 15/11/10.
//  Copyright © 2015年 韩震明. All rights reserved.
//

#import "HZMFeedbackVC.h"
#import <MessageUI/MessageUI.h>

@interface HZMFeedbackVC ()<MFMailComposeViewControllerDelegate, UITextViewDelegate>

@property(nonatomic, retain)UITextView *feedback;
@property(nonatomic, retain)UIButton *button;

@end

@implementation HZMFeedbackVC

- (void)dealloc {
    [_feedback release];
    [_button release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 进入设置按钮
    UIButton *userButton = [UIButton buttonWithType:UIButtonTypeSystem];
    userButton.frame = CGRectMake(self.view.frame.size.width - 35, 20, 25, 25);
    [userButton setTitle:@"返回" forState:0];
    [self.view addSubview:userButton];
    [userButton addTarget:self action:@selector(goToUser) forControlEvents:UIControlEventTouchUpInside];
    
    // 设置输入视图
    self.feedback = [[UITextView alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 30, 110 / 736 * self.view.frame.size.height, self.view.frame.size.width - 40, self.view.frame.size.height / 2 - 110 / 736 * self.view.frame.size.height)];
    self.feedback.layer.borderWidth = 1;
    [self.view addSubview:self.feedback];
    [self.feedback release];
    self.feedback.textAlignment = 0;
    self.feedback.font = [UIFont systemFontOfSize:15];
    self.feedback.layer.cornerRadius = 5;
    self.feedback.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    self.feedback.delegate = self;
    
    // 提交按钮
    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    self.button.frame = CGRectMake(self.view.frame.size.width / 2 - 40, self.view.frame.size.height / 2 + 5, 80, 50);
    [self.view addSubview:self.button];
    [self.button setTitle:@"提交" forState:0];
    [self.button addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];
}




// 准备进入邮箱
- (void)submit {
    if (self.feedback.text.length != 0) {
        Class mailClass = NSClassFromString(@"MFMailComposeViewController");
        if (mailClass) {
            if ([mailClass canSendMail]) {
                [self displayMailBox];
            }
            else {
                [self showMailBoxOnDevice];
            }
        }
        else {
            [self showMailBoxOnDevice];
        }
    }
    else {
        if (self.view.frame.origin.y == -50) {
            [self.feedback resignFirstResponder];
            [UIView animateWithDuration:0.2 animations:^{
                self.view.center = CGPointMake(self.view.center.x, self.view.center.y + 50);
            }];
        }
    }
}

- (void)displayMailBox {
    // 创建邮件视图
    MFMailComposeViewController *mailBox = [[[MFMailComposeViewController alloc] init] autorelease];
    // 添加收件地址
    NSArray *recipient = [NSArray arrayWithObject:@"theooangel@hotmail.com"];
    [mailBox setToRecipients:recipient];
    // 填写主题
    [mailBox setSubject:@"意见反馈"];
    // 邮件内容
    NSString *messageBody = self.feedback.text;
    [mailBox setMessageBody:messageBody isHTML:YES];
    // 设置代理人
    mailBox.mailComposeDelegate = self;
    [self presentViewController:mailBox animated:YES completion:^{
        
    }];
}

- (void)showMailBoxOnDevice {
    NSString *mail = @"mailto:theooangel@icolud.com";
    mail = [mail stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:mail]];
}

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    NSString *message;
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"发送成功" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil];
    switch (result) {
        case MFMailComposeResultCancelled:
            message = @"取消发送";
            NSLog(@"%@", message);
            break;
        case MFMailComposeResultSaved:
            message = @"保存草稿";
            NSLog(@"%@", message);
            break;
        case MFMailComposeResultSent:
            message = @"已发送";
            [alert show];
            NSLog(@"%@", message);
            break;
        case MFMailComposeResultFailed:
            message = @"发送失败";
            NSLog(@"%@ 错误信息:%@", message, error);
            break;
    }
    [self dismissViewControllerAnimated:NO completion:^{
        
    }];
}

- (void)goToUser {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
